﻿using System;
using System.Security.Permissions;
using System.Web;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

namespace SiteFox.WebParts.Navigation
{
    [AspNetHostingPermission(SecurityAction.Demand,
      Level = AspNetHostingPermissionLevel.Minimal)]
    [AspNetHostingPermission(SecurityAction.InheritanceDemand,
      Level = AspNetHostingPermissionLevel.Minimal)]

    public class TreeViewWP : WebPart
    {
        TreeView _TreeView = new TreeView();

        #region PublicProperies
        private string _CollapseImageUrl = "";
        [Personalizable(true)]
        [WebBrowsable(true)]
        [WebDescription("Enter path for collapse image")]
        [WebDisplayName("Collapse image url")]
        public string CollapseImageUrl
        {
            get { return _CollapseImageUrl; }
            set { _CollapseImageUrl = value; }
        }

        private string _CssClass = "";
        [Personalizable(true)]
        [WebBrowsable(true)]
        [WebDescription("Enter style class name")]
        [WebDisplayName("CssClass")]
        public new string CssClass
        {
            get { return _CssClass; }
            set { _CssClass = value; }
        }

        private string _ExpandImageUrl = "";
        [Personalizable(true)]
        [WebBrowsable(true)]
        [WebDescription("Enter path for expand image")]
        [WebDisplayName("Expand image url")]
        public string ExpandImageUrl
        {
            get { return _ExpandImageUrl; }
            set { _ExpandImageUrl = value; }
        }

        private bool _ShowLines = false;
        [Personalizable(true)]
        [WebBrowsable(true)]
        [WebDescription("Check if you want to see TreeView lines")]
        [WebDisplayName("Show lines")]
        public bool ShowLines
        {
            get { return _ShowLines; }
            set { _ShowLines = value; }
        }

        private bool _ShowExpandCollapse = true;
        [Personalizable(true)]
        [WebBrowsable(true)]
        [WebDescription("Check if you want to see Collapse/Expand image")]
        [WebDisplayName("Show collapse/expand")]
        public bool ShowExpandCollapse
        {
            get { return _ShowExpandCollapse; }
            set { _ShowExpandCollapse = value; }
        }
        #endregion

        protected override void CreateChildControls()
        {
            Controls.Clear();
            _TreeView = new TreeView();
            _TreeView.CssClass = _CssClass;
            _TreeView.HoverNodeStyle.CssClass = _CssClass + "_HoverNodeStyle";
            _TreeView.LeafNodeStyle.CssClass = _CssClass + "_LeafNodeStyle";
            _TreeView.NodeStyle.CssClass = _CssClass + "_NodeStyle";
            _TreeView.ParentNodeStyle.CssClass = _CssClass + "_ParentNodeStyle";
            _TreeView.RootNodeStyle.CssClass = _CssClass + "_RootNodeStyle";
            _TreeView.SelectedNodeStyle.CssClass = _CssClass + "_SelectedNodeStyle";
            if (_CollapseImageUrl != "") { _TreeView.CollapseImageUrl = _CollapseImageUrl; };
            if (_ExpandImageUrl != "") { _TreeView.ExpandImageUrl = _ExpandImageUrl; };
            _TreeView.ShowLines = _ShowLines;
            _TreeView.ShowExpandCollapse = _ShowExpandCollapse;
            _TreeView.Nodes.Clear();
            var _RootNodes = SiteFox.Business.Manager.SiteMapNode.GetRootNodes();
            for (int i = 0; i < _RootNodes.Count; i++)
            {
                var _RootNode = _RootNodes[i];
                TreeNode _tn = new TreeNode(_RootNode.Title, _RootNode.ID.ToString());
                this.LoadChildNodes(_tn);
                _TreeView.Nodes.Add(_tn);
            }
            Controls.Add(_TreeView);
        }
        void LoadChildNodes(TreeNode _ParentNode)
        {
            var _ChildNodes = SiteFox.Business.Manager.SiteMapNode.GetByParentNode(Convert.ToInt32(_ParentNode.Value));
            for (int i = 0; i < _ChildNodes.Count; i++)
            {
                var _ChildNode = _ChildNodes[i];
                TreeNode _tn = new TreeNode(_ChildNode.Title, _ChildNode.ID.ToString());
                this.LoadChildNodes(_tn);
                _ParentNode.ChildNodes.Add(_tn);
            }
        }
        void ExpandParentNode(TreeNode _Node)
        {
            _Node.Expand();
            if (_Node.Parent != null) 
            {
                this.ExpandParentNode(_Node.Parent);
            }
        }
    }
}
